using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using PSLib = Microsoft.Office.Project.Server.Library;

namespace DataPopulation
{
    public partial class CleanupDlg : Form
    {
        public CleanupDlg()
        {
            InitializeComponent();
        }

        private void btnRemoveResources_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                btnRemoveResources.Enabled = false;

                // Filter list based upon the prefix.
                string filterXml = Helper.GetResourceFilter(txtDeleteResPrefix.Text);
                wsResource.ResourceDataSet dsResource = Helper.resource.ReadResources(filterXml, false);

                // Filter further based upon the start and end range.
                int start = Convert.ToInt32(txtDeleteResStart.Text);
                int end = Convert.ToInt32(txtDeleteResEnd.Text);

                string resName;
                ArrayList resList = new ArrayList();
                for (int index = start; index <= end; index++)
                {
                    resName = txtDeleteResPrefix.Text + Helper.GetDigits(index, Convert.ToInt32(txtDeleteResDigits.Text));

                    DataView dataview = new DataView(dsResource.Resources);
                    dataview.RowFilter = dsResource.Resources.RES_NAMEColumn + " = '" + resName + "'";
                    if (dataview.Count == 0) continue;

                    wsResource.ResourceDataSet.ResourcesRow rowRes = (wsResource.ResourceDataSet.ResourcesRow)dataview[0].Row;

                    resList.Add(rowRes.RES_UID);
                    // MessageBox.Show("Added resource " + rowRes.RES_NAME + ".");
                }
                if (resList.Count == 0)
                {
                    MessageBox.Show("No matching resources were found.");
                    return;
                }

                Guid[] resources = new Guid[resList.Count];
                for (int index = 0; index < resList.Count; index++)
                {
                    resources[index] = (Guid)resList[index];
                }

                try
                {
                    Helper.resource.CheckOutResources(resources);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("CheckOutResources failed. " + ex.Message);
                }
                try
                {
                    Helper.resource.DeleteResources(resources, "Deleted by Data Population Tool");
                    MessageBox.Show(resList.Count + " matching resources were removed.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("DeleteResources failed. " + ex.Message);
                    Helper.resource.CheckInResources(resources, true);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                btnRemoveResources.Enabled = true;
                Cursor = Cursors.Default;
            }
        }

        private void btnRemoveProjects_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                btnRemoveProjects.Enabled = false;

                wsProject.ProjectDataSet dsProject = Helper.project.ReadProjectStatus(Guid.Empty, wsProject.DataStoreEnum.WorkingStore, "", (int)PSLib.Project.ProjectType.Project);
                if (dsProject == null)
                {
                    MessageBox.Show("ReadProjectStatus failed during Remove.");
                    return;
                }

                // Filter further based upon the start and end range.
                int start = Convert.ToInt32(txtDeleteProjStart.Text);
                int end = Convert.ToInt32(txtDeleteProjEnd.Text);

                string projName;
                ArrayList projList = new ArrayList();
                for (int index = start; index <= end; index++)
                {
                    projName = txtDeleteProjPrefix.Text + Helper.GetDigits(index, Convert.ToInt32(txtDeleteProjDigits.Text));

                    DataView dataview = new DataView(dsProject.Project);
                    dataview.RowFilter = dsProject.Project.PROJ_NAMEColumn + " = '" + projName + "'";
                    if (dataview.Count == 0) continue;

                    wsProject.ProjectDataSet.ProjectRow rowProj = (wsProject.ProjectDataSet.ProjectRow)dataview[0].Row;

                    projList.Add(rowProj.PROJ_UID);
                }
                if (projList.Count == 0)
                {
                    MessageBox.Show("No matching projects were found.");
                    return;
                }

                Guid[] projects = new Guid[projList.Count];
                for (int index = 0; index < projList.Count; index++)
                {
                    projects[index] = (Guid)projList[index];
                }

                try
                {
                    Guid jobGuid = Guid.NewGuid();
                    Helper.project.QueueDeleteProjects(jobGuid, false, projects, true);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("QueueDeleteProjects failed. " + ex.Message);
                }
                MessageBox.Show(projList.Count + " matching projects were removed.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                btnRemoveProjects.Enabled = true;
                Cursor = Cursors.Default;
            }
        }
    }
}